package lesson.Objectstream;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class ObjectStreamDemo3 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day12\\src\\lesson\\Objectstream\\b.txt"));

        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("张三","23");
        Student stu2 = new Student("李四","23");
        Student stu3 = new Student("王五","21");

        Collections.addAll(list,stu1,stu2,stu3);

        oos.writeObject(list);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Day12\\src\\lesson\\Objectstream\\b.txt"));

        ArrayList<Student> newList = (ArrayList<Student>) ois.readObject();
        for (Student student : newList) {
            System.out.println(student);
        }


        ois.close();
    }
}
